package com.yonyou.brigade.cron.order.didi;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.dangdang.ddframe.job.api.ShardingContext;
import com.dangdang.ddframe.job.api.simple.SimpleJob;
import com.yonyou.brigade.basic_init.enums.ChannelEnum;
import com.yonyou.brigade.basic_init.model.BriChannel;
import com.yonyou.brigade.basic_init.model.Developer;
import com.yonyou.brigade.basic_init.service.IBriChannelService;
import com.yonyou.brigade.basic_init.service.IDeveloperService;
import com.yonyou.brigade.third_parties.didi.model.DiDiConfig;
import com.yonyou.brigade.third_parties.didi.service.IDiDiOrderService;
import com.yonyou.brigade.util.operateutil.DateUtils;
import com.yonyou.ybzboot.starter.elasticjob.lite.annotation.ElasticJobConfig;
import com.yonyou.ybzboot.starter.mail.model.Email;
import com.yonyou.ybzboot.starter.mail.service.IMailService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;

import java.util.List;

@ElasticJobConfig(cron = "0 50 3 * * ? ", shardingTotalCount = 1)
public class FetchDiDiCarOrders implements SimpleJob {

    Logger log= LoggerFactory.getLogger(this.getClass());

    @Autowired
    @Qualifier("briChannelServiceImpl")
    private IBriChannelService briChannelService;

    @Autowired
    @Qualifier("diDiOrderServiceImpl")
    private IDiDiOrderService diDiOrderService;

    @Autowired
    @Qualifier("developerServiceImpl")
    private IDeveloperService developerService;

    @Autowired
    private IMailService mailService;

    @Value("${brigade.cur}")
    private String curEnv;

    @Override
    public void execute(ShardingContext shardingContext) {
        log.info("<<<<didi-car-pull-orders-begin>>>>");
        List<BriChannel> briChannels = briChannelService.getEnableChannelByChannel(ChannelEnum.DIDI.getName());
        for (BriChannel briChannel : briChannels) {
            String tenant = briChannel.getTenant();
            DiDiConfig diDiConfig = (DiDiConfig)briChannelService.getChannelConfig(tenant, ChannelEnum.DIDI.getName(), new DiDiConfig());
            String fetchDate = DateUtils.getCurrentDateMinusOneDay();
            try {
                diDiOrderService.getDiDiOrderList(diDiConfig,fetchDate,tenant,briChannel.getId());
            } catch (Exception e) {
                Wrapper<Developer> developerWrapper = new EntityWrapper<>();
                developerWrapper.where("type={0}", "KF");
                List<Developer> developers = developerService.selectList(developerWrapper);
                String developerEmails = "";
                for (int j = 0; j < developers.size(); j++) {
                    developerEmails += developers.get(j).getEmail();
                    if (j < developers.size() - 1) {
                        developerEmails += ",";
                    }
                }
                Email email = new Email();
                String[] emails = developerEmails.split(",");
                email.setEmail(emails);
                email.setSubject(curEnv + "环境滴滴用车订单拉单失败");
                email.setContent(e.getMessage());
                try {
                    log.error(e.getMessage());
                    mailService.send(email);
                } catch (Exception e1) {
                    log.error("提示" + curEnv + "环境滴滴用车订单拉单失败的邮件发送失败！" + e1.getMessage());
                }
            }
        }
        log.info("<<<<didi-car-pull-orders-end>>>>");
    }
}
